#include "ChcSceneManager.hpp"
#include "SceneNode.hpp"
#include "Camera.hpp"
#include "OpenGL.hpp"


namespace cge {
	struct ChcSceneManager::Node: public SceneNode {
		Node( ChcSceneManager& m ): SceneNode(m) {}
		Node( Node& p )           : SceneNode(p) {}

		virtual void performUpdate() {

		}

		virtual Node* createChild() {
			return new Node(*this);
		}
	};

	ChcSceneManager::ChcSceneManager( Engine &e )
		: SceneManager(e) {

		root_.reset( new Node(*this) );
	}

	ChcSceneManager::~ChcSceneManager() {}

	SceneNode& ChcSceneManager::getRoot() { return *root_; }

	// rendering
	void ChcSceneManager::setCamera( Camera& camera ) {
		cam_ = &camera;
	}

	void ChcSceneManager::addRenderable( SceneNode& node ) {

	}


	void ChcSceneManager::removeRenderable( SceneNode& node ) {

	}

	void ChcSceneManager::runTask() {
		if( !cam_ || ! cam_->isAttached() )
			return;
		vec3 campos = cam_->getNode().getPosition();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		float lightPos[4] = { (float)campos.x, (float)campos.y, (float)campos.z, 1.0f };
		float lightAmb[4] = { 0.05f, 0.05f, 0.05f, 1.0f };
		float lightDif[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
		glLightfv( GL_LIGHT0, GL_POSITION, lightPos );
		glLightfv( GL_LIGHT0, GL_AMBIENT, lightAmb );
		glLightfv( GL_LIGHT0, GL_DIFFUSE, lightDif );
		glLightf( GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.01f );
		cam_->applyView();
		root_->renderAll();
	}
}